varying vec2 _texture;
varying vec3 _position;

uniform vec4 env_screenSize;
uniform vec3 env_cameraPos;
uniform vec2 water_translation;
uniform float env_visualRange;
uniform float env_fogLength;
uniform float time;
uniform int waterDrawMode;
uniform sampler2D tex_water1;
uniform sampler2D tex_water2;
uniform sampler2D tex_reflection;
uniform sampler2D tex_underwater;
uniform sampler2D tex_hmap;

void main(void) {
	//int isUnderwater = 0;

	//razy 8 bo tyle jest skalowana woda
	float dist = distance(water_translation + _position.xz * 8.0,
			env_cameraPos.xz);

	/*
	 * Duże czarymary z teksturami
	 */

	vec4 perlin1_1 = texture2D(tex_water1, vec2(_texture.x * 16 + 3 * sin(time
			/ 10000), _texture.y * 16 + 3 * cos(time / 10000)));

	vec4 perlin1_2 = texture2D(tex_water1, vec2(_texture.x * 1024 + 3 * sin(
			time / 10000), _texture.y * 1024 + 3 * cos(time / 10000)));

	vec4 perlin1_3 = texture2D(tex_water1, _texture);

	vec4 perlin2_1 = texture2D(tex_water2, vec2(_texture.x * 32 + 3 * sin(time
			/ 10000), _texture.y * 32 + 3 * cos(time / 10000)));

	vec4 perlin2_2 = texture2D(tex_water2, _texture * 2 + 0.3 * sin(time
			/ 10000));

	vec4 refTexture = texture2D(tex_reflection, vec2((gl_FragCoord.x - 20 + 40
			* perlin1_1.r) / env_screenSize.x, (gl_FragCoord.y - 20 + 40
			* perlin1_1.r) / env_screenSize.y));

	vec4 uwTexture = texture2D(tex_underwater, vec2((gl_FragCoord.x - 20 + 40
			* perlin2_1.r) / env_screenSize.x, (gl_FragCoord.y - 20 + 40
			* perlin2_1.r) / env_screenSize.y));

	/*
	 * Akcja właściwa
	 */

	vec4 result = (0.4 * uwTexture + 0.6 * refTexture);

	result.b = (result.b * 0.7 + 0.3 * result.b * perlin2_1.x * perlin2_2.x);// / 4;

	/*
	 * Przezroczystość przy kamerze jest zwiększana
	 */

	/*
	 * Przezroczystość wzrasta na krawędzi widzenia
	 */
	//if (dist + env_fogLength > env_visualRange) {
	result.a = min(max((env_visualRange - dist) / env_fogLength, 0.0), 1.0);
	//}

	//result.a = 1.0;

	gl_FragColor = waterDrawMode * (0.95*result - 0.1*perlin1_3);// + (1-waterDrawMode) * perlin2_1.x;// * perlin1_1.x;

}
